@extends('layouts.appManager')
@section('title')配置文件管理@endsection
@section('style')
    <style>
        .path{
            font-size: 12px;
            color: darkgray;
        }
        .name{
            font-size: 16px;
            font-weight: bold;
        }
    </style>
@endsection
@section('content')
    <input type="hidden" id="serverId" value="{{$server->id}}">
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-12">
                <div class="card panel mb-4">
                    <div class="card-header">配置文件管理</div>

                    <div class="card-body ">
                        <div class="text-right">
                            <el-button type="success" size="small" @click="resetConf()">重载配置</el-button>
                            <el-button type="success" size="small" @click="addAuto()">自动获取</el-button>
{{--                            <el-button type="success" size="small" @click="addShow()">添加</el-button>--}}
                        </div>

                        <el-table
                            :data="tableData"
                            style="width: 100%">
                            <el-table-column
                                prop="path"
                                label="模块">
                                <template slot-scope="scope">
                                    <span class="name">@{{ scope.row.path | showFileName }}</span><br>
                                    <span class="path">@{{ scope.row.path }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="操作">
                                <template slot-scope="scope">
{{--                                    <el-button size="mini" type="primary" @click="edit(scope.row)">编辑</el-button>--}}
                                    <el-button size="mini" type="primary" @click="config(scope.row)">配置</el-button>
                                    <el-button size="mini" type="danger" @click="del(scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <el-dialog v-if="curItem" title="添加配置文件" :show-close="false" :visible.sync="dialog" :close-on-click-modal="false">
        <el-form label-width="80px" >
            <el-form-item label="文件路径">
                <el-input v-model="curItem.path"></el-input>
                <small class="text-secondary"> <i class="fas fa-info-circle"></i> 请输入配置文件路径，包括文件名 </small>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialog=false">取 消</el-button>

            <el-button v-if="curItem.id"  type="primary" @click=" update() ">修 改</el-button>
            <el-button v-else type="primary" @click=" add() ">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog v-if="curItem" :title="curItem.path|showFileName" :show-close="true" :visible.sync="dialogConf" :close-on-click-modal="true">
        <div style="text-align: center">
            <el-radio-group v-model="cat" size="mini" v-if="cats.length>1">
                <el-radio-button v-for="v in cats" :label="v">@{{ v?v:'其他' }}</el-radio-button>
            </el-radio-group>
        </div>

        <br>
        <el-form label-width="400px" >

            <el-form-item v-for=" (v,k) in confList" :key="k" :label="v.info.label" v-if="cats.length<=1 || v.info.cat===cat">

{{--                <el-input v-if="v.info.type=='number'&&!v.info.min&&!v.info.max"--}}
{{--                          v-model="v.value" type="number" min="0"></el-input>--}}
                <el-switch size="mini"
                    v-if="v.info.type==='bool'"
                    v-model="v.value"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                </el-switch>
                <el-input v-else-if="v.info.type==='number'"  size="mini"
                          v-model="v.value" type="number" :min="v.info.min" :max="v.info.max" :step="v.info.step?v.info.step:(v.info.min===0.1?0.1:1)"></el-input>
{{--                <el-slider v-else-if="v.info.type=='number'" :min="v.info.min" :max="v.info.max" :step="v.info.min==0.1?0.1:(v.info.step?v.info.step:1)" v-model="v.value" show-input></el-slider>--}}
                <el-select v-else-if="v.info.type==='select'" v-model="v.value"  size="mini">
                    <el-option
                        v-for="op in v.info.options"
                        :key="op.value"
                        :label="op.label"
                        :value="''+op.value">
                    </el-option>
                </el-select>
                <el-input size="mini" v-else-if="v.info.type==='textarea'" :type="v.info.type" :rows="20" v-model="v.value"></el-input>
                <el-input size="mini" v-else :type="v.info.type" v-model="v.value"></el-input>

            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogConf=false">取 消</el-button>
            <el-button type="primary" @click=" confSave() ">保存</el-button>
        </div>
    </el-dialog>

@endsection
@section('script')
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                serverId: $('#serverId').val(),
                tableData: [],
                curItem:null,
                dialog:false,
                dialogConf:false,
                confList:[],
                cats:[],
                cat:'',
                modNames:{
                    'worldserver':'【游戏服务器配置】',
                    'CFBG':'战场混排',
                    'Anticheat':'防作弊',
                    'mod_ahbot':'拍卖行机器人',
                    'SkipDKModule':'DK跳过新手任务',
                    'mod-bg-item-reward':'战场奖励',
                    'BGReward':'战场奖励',
                    'mod_LuaEngine':'LUA',
                    'mod_learnspells':'自动学习技能',
                    'progression_system':'渐进系统',
                    'authserver':'登录服务器配置',
                    'transmog':'幻化',
                }
            },
            mounted() {
                this.getList();
            },
            filters:{
                //处理函数
                showFileName(path){
                    let modNames=app.$data.modNames
                    let pos=path.lastIndexOf('/');
                    let name= path.substr(pos+1);
                    if (modNames[name])
                        return modNames[name]
                    else
                        return ''
                }
            },
            methods:{
                getList(){
                    post('list/'+this.serverId,{},res=>{
                        console.log(res);
                        this.tableData=res;
                    },this)
                },
                addShow(){
                    if (this.tableData.length>=60){
                        this.$message.info('最多添加60个视频');
                        return;
                    }
                    this.curItem={};
                    this.dialog=true;
                },
                add(){
                    if (this.curItem.path){
                        this.adddo();
                    } else {
                        this.$message.error('请输入配置文件全路径');
                    }
                },
                adddo(){
                    post('add/'+this.serverId,this.curItem,res=>{
                        console.log(res);
                        if (res.err===0) {
                            this.$message.success('添加成功');
                            this.dialog=false;
                            this.getList();
                        }else{
                            this.$message.error(res.msg);
                        }
                    },this)
                },
                config(item){
                    this.curItem=item;
                    post('conf/'+this.serverId,{id:this.curItem.id},res=>{
                        console.log(res);
                        if (res.err===0) {
                            this.confList=res.data;
                            this.cats=[];
                            for(let key in this.confList){
                                let e=this.confList[key]['info'];
                                if (e.cat && e.cat!=='' && this.cats.indexOf(e.cat)===-1){
                                    this.cats.push(e.cat);
                                }
                                if (!e.cat) e.cat='';
                            }
                            this.cats.push('');
                            this.cat=this.cats.length>0?this.cats[0]:'';
                            this.dialogConf=true;
                        }else{
                            this.$message.error(res.msg);
                        }
                    },this)
                },
                confSave(){
                    let confList= "";
                    for(let key in this.confList){
                        confList+=(key+'='+this.confList[key]['value']+"\n")
                    }
                    post('confSave/'+this.serverId+'/'+this.curItem.id,{data:confList},res=>{
                        console.log(res);
                        if (res.err===0) {
                            this.$message.success('保存成功');
                        }else{
                            this.$message.error(res.msg);
                        }
                    },this)
                },
                del(item){
                    this.$confirm('此操作将删除此配置, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        console.log(1);
                        post('del/'+this.serverId+'/'+item.id,{},res=>{
                            console.log(res);
                            if (res.err===0){
                                this.$message.success('删除成功');
                                this.getList();
                            } else{
                                this.$message.error(res.msg);
                            }
                        },this)
                    }).catch(() => {
                        // this.$message({
                        //     type: 'info',
                        //     message: '已取消删除'
                        // });
                    });
                },

                update(){
                    post('/manager/server/'+this.serverId+'/videoUpdate',this.curItem,res=>{
                        console.log(res);
                        if (res.err===0) {
                            this.$message.success('修改成功');
                            this.dialog=false;
                        }else {
                            this.$message.error(res.msg);
                        }
                    },this);
                },
                edit(item){
                    this.curItem=item;
                    this.dialog=true;
                },
                addAuto(){
                    var t=this;
                    post('addAuto/'+this.serverId,{},res=>{
                        if (res.data===1){
                            t.getList();
                            t.$message({
                                type: 'success',
                                message: '执行成功，添加了 '+res.mag+' 个配置'
                            });
                        }else{
                            t.$message({
                                type: 'error',
                                message: res.info
                            });
                        }
                        console.log(res.data);
                    },t);
                },
                resetConf(){
                    var t=this;
                    post("/manager/server/config/runGMCommand",{id:t.serverId,command:'reload config'},res=>{
                        if (res.data===1){
                            t.$message({
                                type: 'success',
                                message: '重载成功，部分配置可能需要重启服务端'
                            });
                        }else{
                            t.$message({
                                type: 'error',
                                message: res.info
                            });
                        }
                        console.log(res.data);
                    },t);
                }
            }
        });
    </script>
@endsection


